//
//  UIAlertController+Extension.swift
//  Advance
//
//  Created by 松松 on 2018/9/20.
//  Copyright © 2018年 整物搭链. All rights reserved.
//

import Foundation
import UIKit

extension UIAlertController{
    static public func creat(title:String?,message:String?,style:UIAlertControllerStyle) -> UIAlertController{
        let alert = UIAlertController(title: title, message: message, preferredStyle: style)
        return alert
    }

    public func addAction(title:String,style:UIAlertActionStyle = .default,handle:(()->Void)?) -> UIAlertController{
        let act = UIAlertAction(title: title, style: style) { (act) in
            handle?()
        }
        addAction(act)
        return self
    }

    //适配ipad
    //如果style为sheet  sender不能为空
    public func showOn(_ viewController:UIViewController,_ sender:UIView? = nil){
        if UIDevice.current.userInterfaceIdiom == .pad && self.preferredStyle == .actionSheet{
            let popPresenter = self.popoverPresentationController
            popPresenter?.sourceView = sender
            popPresenter?.sourceRect = sender!.bounds
        }
        viewController.present(self, animated: true, completion: nil)
    }

    public func getContentLabel() -> UILabel?{
//        _UIInterfaceActionGroupHeaderScrollView
        var labels = [UILabel]()
        let subviews = view.ts_allSubviews()
        for view in subviews{
            if view.isKind(of: UILabel.self){
                labels.append(view as! UILabel)
            }
        }
        return labels.last
    }
}

